package cn.zhage.aimin.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


/*
 *
 * @author 渣哥
 */
@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
public class ChatController {

    private final ChatMemory chatMemory;

    private final ChatClient chatClient;

    @GetMapping("/qa")
    public String generateChatClient(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return chatClient.prompt(message)
                .advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, 1))
                .call().content();
    }

    @GetMapping(value = "/stream")
    public Flux<String> chat(@RequestParam("message") String message) {
        return chatClient
                .prompt()
                .system("你会根据用户的需求，查询表名，和表中对应字段，并生成sql,查询数据.")
                .user(message)
                .advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, 2))
                .stream().content();
    }
}
